package com.widget;

import java.util.ArrayList;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class MilkDrink extends View {
	private final Paint mGesturePaint = new Paint();
	private final Path mPath = new Path();

	private ArrayList<Point> point = new ArrayList<Point>();
	private ArrayList<Point> save = new ArrayList<Point>();

	public MilkDrink(Context context) {
		super(context);
	}

	public MilkDrink(Context context, AttributeSet attrs) {
		super(context, attrs);
		mGesturePaint.setAntiAlias(true);
		mGesturePaint.setStyle(Style.FILL);
		mGesturePaint.setStrokeWidth(5);
		mGesturePaint.setColor(Color.WHITE);
	}

	public MilkDrink(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		function_Catmull_Rom(point, 100, save, mPath);
		canvas.drawPath(mPath, mGesturePaint);
	}

	public void function_Catmull_Rom(ArrayList<Point> point, int cha,
			ArrayList<Point> save, Path path) {
		if (point.size() < 4) {
			return;
		}
		path.moveTo(point.get(0).x, point.get(0).y);
		save.add(point.get(0));
		for (int index = 1; index < point.size() - 2; index++) {
			Point p0 = point.get(index - 1);
			Point p1 = point.get(index);
			Point p2 = point.get(index + 1);
			Point p3 = point.get(index + 2);

			for (int i = 1; i <= cha; i++) {
				float t = i * (1.0f / cha);
				float tt = t * t;
				float ttt = tt * t;

				Point pi = new Point(); // intermediate point
				pi.x = (float) (0.5 * (2 * p1.x + (p2.x - p0.x) * t
						+ (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * tt + (3
						* p1.x - p0.x - 3 * p2.x + p3.x)
						* ttt));
				pi.y = (float) (0.5 * (2 * p1.y + (p2.y - p0.y) * t
						+ (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * tt + (3
						* p1.y - p0.y - 3 * p2.y + p3.y)
						* ttt));
				path.lineTo(pi.x, pi.y);
				save.add(pi);
				pi = null;
			}
		}
		path.lineTo(point.get(point.size() - 1).x,
				point.get(point.size() - 1).y);
		save.add(point.get(point.size() - 1));
	}
	
	public void setPoint(JSONArray jsonArray,int height,int width,int milk,int index)
	{
		point.clear();
		
		JSONObject json = jsonArray.getJSONObject(index);
		
		JSONArray array = json.getJSONArray("data");
		int count = array.size() + 1;
		int offset = width/(count - 1);
		
		for(int i = 0; i < count; i++)
		{
			if(i == count - 1)
			{
				if(index < jsonArray.size() - 1)
				{
					JSONArray list = jsonArray.getJSONObject(index + 1).getJSONArray("data");
					if(list.size() > 0)
					{
						int goal = list.getJSONObject(0).getIntValue("consumed_ml");
						int yPos = height - goal*height/milk;
						point.add(new Point(width, yPos));
					}
				}
				
				point.add(new Point(width, height));
			}
			else
			{
				int goal = array.getJSONObject(i).getIntValue("consumed_ml");
				int yPos = height - goal*height/milk;

				if(i == 0) point.add(new Point(0, height));
				point.add(new Point(offset*i, yPos));
			}
		}
		
		invalidate();
	}
}
